<template>
  <el-card shadow="always" align="center">
    <!--1. 条件查询-->
    <el-button type="primary" style="float:left" size="medium" plain @click="addUI">添加规格</el-button>
    <el-table
      :data="specList"
      style="width: 98%">
      <el-table-column
        label="规格ID"
        prop="id"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        label="规格名称"
        prop="specName"
        align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--打开规格对话框-->
    <el-dialog title="编辑规格" :visible.sync="dialogFormVisible" align="left" :close-on-click-modal="false">
      {{spec}}
      <el-form :model="spec">
        <el-form-item label="规格名称" :label-width="formLabelWidth">
          <el-input v-model="spec.specification.specName" autocomplete="off"></el-input>
        </el-form-item>
        <el-button type="success" @click="spec.options.push({})" size="medium" plain>新增规格选项</el-button>
      </el-form>
      <el-table
        :data="spec.options"
        align="center"
        style="width: 100%">
        <el-table-column
          label="规格选项"
          align="center">
          <template slot-scope="scope">
           <el-input v-model="scope.row.optionName"/>
          </template>
        </el-table-column>
        <el-table-column
          label="排序"
          align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.orders"/>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="spec.options.splice(scope.$index,1)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import { findAll ,findSpecById,save,delSpec} from '@/api/specification'

export default {
  data() {
    return {
      specList:[],
      dialogFormVisible:false, // 默认隐藏对话框
      spec:{specification:{},options:[]},
      formLabelWidth:'100px',

    }
  },
  created() {
     //1. 查询所有规格
     this.findSpecList()
  },
  methods: {
    //1. 查询所有规格
    findSpecList(){
       findAll().then(({data}) => {
         this.specList = data.list
       })
    },
    //2. 新增规格
    addUI(){
      //2.1 清空组合对象
      this.spec = {options: [],specification:{}}
      //2.2  打开对话框
      this.dialogFormVisible = true
    },
    //3. 提交表单
    submitForm(){
      //3.1 根据是否有规格id，决定是添加还是修改操作
      let url = "/specification/add.do"
      if(this.spec.specification.id){
        url = "/specification/update.do"
      }
      //3.2 提交到后台
      save(this.spec,url).then((data) => {
        console.log("data:",data)
        let _this = this
        if(data.code === 20000){
          this.$message({
            message: data.message,
            type: 'success',
            onClose(){
              _this.dialogFormVisible = false
              _this.findSpecList()
            }
          });
        }
      })
    },
    handleEdit(index, row) {
      console.log(index, row);
      //1. 根据规格id，在后台查询出规格及规格选项的组合对象
      findSpecById(row.id).then(({data}) => {
         this.spec = data.spec
      })
      //2. 弹出对话框
      this.dialogFormVisible = true
    },
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm(`你是真的要删除[${row.specName}]吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delSpec(row.id).then(data => {
          let _this = this
          if(data.code === 20000){
            this.$message({
              type: 'success',
              message: data.message,
              onClose(){
               _this.findSpecList()
              }
            });
          }
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    }
  }
}
</script>
